package com.nanfang51g3.ckz.ui.money.fill;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.ui.fragment.fill.FillInAliPayFragment;
import com.nanfang51g3.ckz.ui.fragment.fill.FillInBankFragment;
import com.nanfang51g3.ckz.ui.money.add.bank.AddBankActivity;
import com.nanfang51g3.ckz.ui.money.add.pay.AddALiPayActivity;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/4/19.
 */
public class FillInActivity extends Base2Activity implements FillInContract.View, View.OnClickListener {
    FillInContract.FillInActionsListener fillInActionsListener;
    @Bind(R.id.index_tv_right_title)
    TextView indexTvRightTitle;
    @Bind(R.id.rb_yes)
    RadioButton rbYes;
    @Bind(R.id.rb_no)
    RadioButton rbNo;
    @Bind(R.id.index_tv_title)
    TextView indexTvTitle;
    int type = 0;
    @Override
    public IPresenter getPresenter() {
        return null;
    }

    private FragmentManager fragmentManager;
    private Fragment currentFragment;
    private RadioButton[] rbs = new RadioButton[2];

    @Override
    public int getContentView() {
        return R.layout.fill_in_money;
    }

    @Override
    public boolean isShowLeftIcon() {
        return true;
    }

    @Override
    public boolean isShowRightIcon() {
        return false;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_accounting_left;
    }

    @Override
    public int setRightIcon() {
        return 0;
    }

    @Override
    public int showTitle() {
        return R.string.str_fill_in_bank;
    }

    @Override
    public void onClickLeft(View v) {
        AppManager.getAppManager().finishActivity(this);
    }

    @Override
    public void onClickRight(View v) {
        EasyToast.showShort(this, "msg");
    }


    @OnClick(R.id.index_tv_right_title)
    public void index_tv_right_title(View view) {
        switch (type) {
            case 0:
                startActivity(new Intent(this, AddBankActivity.class));
                break;
            case 1:
                startActivity(new Intent(this, AddALiPayActivity.class));
                break;
        }

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fillInActionsListener = DaggerFillInContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .fillInPresenterModule(new FillInPresenterModule(this))
                .build().getFillInPresenter();
        KLog.i("TAG", fillInActionsListener == null ? "fillInActionsListener==null" : "fillInActionsListener!=null");
        ButterKnife.bind(this);
        fragmentManager = this.getSupportFragmentManager();
        rbs[0] = rbYes;
        rbs[1] = rbNo;
        rbs[0].setOnClickListener(this);
        rbs[1].setOnClickListener(this);
        defaultInit();

    }

    FillInBankFragment addFragment;
    FillInAliPayFragment bddFragment;

    //　默认view
    public void defaultInit() {
        FragmentTransaction transaction = this.fragmentManager
                .beginTransaction();//获取Fragment事务
        addFragment = FillInBankFragment.newInstance(this, fillInActionsListener);//创建Fragment
        transaction.add(R.id.content_frame, addFragment);//查找layout FrameLayout 替换fragment
        currentFragment = addFragment;
        transaction.commit();
    }

    @Override
    public void onClick(View view) {
        FragmentTransaction transaction = this.fragmentManager.beginTransaction();
        switch (view.getId()) {
            case R.id.rb_yes:
                type = 0;
                if (addFragment == null) {
                    addFragment = FillInBankFragment.newInstance(this, fillInActionsListener);//创建Fragment
                }

                indexTvTitle.setText("银行卡");
                switchContent(addFragment);
                break;

            case R.id.rb_no:
                type = 1;
                if (bddFragment == null) {
                    bddFragment = FillInAliPayFragment.newInstance(this, fillInActionsListener);//创建Fragment
                }
                indexTvTitle.setText("支付宝");
                switchContent(bddFragment);
                break;
        }
        transaction.commit();
        // Replace whatever is in thefragment_container view with this fragment,
        // and add the transaction to the backstack
    }

    public void switchContent(Fragment fragment) {
        //记录第一个初始化Fragment不等于传进的的Fragment
        if (currentFragment != fragment) {
            FragmentTransaction transaction = getSupportFragmentManager()
                    .beginTransaction();
            if (!fragment.isAdded()) {//传入的Fragment未添加过 隐藏当前添加新的Fragment
                transaction.hide(currentFragment).add(R.id.content_frame, fragment).commit();
            } else {
                transaction.hide(currentFragment).show(fragment).commit();//隐藏当前，显示传入的Fragment
            }
            currentFragment = fragment;//记录操作的fragment
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);//解除绑定，官方文档只对fragment做了解绑
    }

}
